package com.cindata

import com.cindata.dictionary.BuildingTypeDict
import com.cindata.dictionary.ExecStatusDict

import javax.xml.transform.Source

class UserCost
{

    User user
    // 操作用户
    Fees fees
    // 操作类型
    FeesDetail feesDetail
    // 收费标准
    BuildingTypeDict buildingType
    // 操作对象类型
    Integer batchId
    // 所属批次ID
    Integer sourceId
    // 源对象ID
    String sourceClass
    // 源对象ClassName
    BigDecimal amount
    // 花费金额
    String description
    // 花费描述
    ExecStatusDict status
    // 状态
    Date finishAt
    // 操作完成时间

    Date createdDate = new Date()
    Date modifiedDate = new Date()
    User createBy
    User modifyBy

    UserCost()
    {
    }

    // 根据单个押品估值创建估值消费记录
    static CreateByEstateEvaluate(Long batchId, EstateImport estateItem)
    {
        def f = Fees.findByCode('EstateEvaluate')
        def fd = FeesDetail.findByFeesAndBuildingType(f, estateItem.buildingType)
        def currentUser = estateItem.getCreateBy()
        def userCost = new UserCost()
        userCost.user = currentUser
        userCost.fees = f
        userCost.sourceId = estateItem.getId()
        userCost.sourceClass = estateItem.getClass().toString()
        userCost.batchId = batchId
        userCost.buildingType = estateItem.buildingType
        def unitPirce = 0
        if (fd)
        {
            userCost.feesDetail = fd
            userCost.status = ExecStatusDict.EXECUTING()
            unitPirce = fd.unitPrice
        }
        else
        {
            userCost.status = ExecStatusDict.FAILURE()
        }
        userCost.amount = 0
        userCost.description = userCost.getEstateEvaluateDescription(batchId, estateItem, unitPirce)
        userCost.createBy = currentUser
        userCost.save(flush: true)
        return userCost
    }

    static belongsTo = [feesDetail: FeesDetail, fees: Fees]

    static constraints = {
        user nullable: true, blank: true, comment: '操作用户'
        fees nullable: true, blank: true, comment: '操作类型'
        feesDetail nullable: true, blank: true, comment: '收费标准'
        buildingType nullable: true, blank: true, comment: '操作对象类型'
        batchId nullable: true, blank: true, comment: '批次编号'
        sourceId nullable: true, blank: true, comment: '源对象ID'
        sourceClass nullable: true, blank: true, maxSize: 100, comment: '源对象ClassName'
        amount precision: 15, scale: 2, nullable: true, blank: true, comment: '花费金额'
        description maxSize: 255, nullable: true, blank: true, comment: '花费描述'
        status nullable: true, blank: true, comment: '状态'
        finishAt nullable: true, blank: true, comment: '操作完成时间'
        createBy nullable: true, blank: true, comment: '创建者'
        modifyBy nullable: true, blank: true, comment: '修改者'
        createdDate nullable: true, blank: true, comment: '创建时间'
        modifiedDate nullable: true, blank: true, comment: '修改时间'
    }

    // 获得估值操作的描述
    def getEstateEvaluateDescription(Long batchId, EstateImport estateItem, Double unitPirce)
    {
        if (feesDetail)
        {
            return "对押品“${batchId}-${estateItem?.address}”进行估值，预计收费${unitPirce}元"
        }
        else
        {
            return "对押品“${batchId}-${estateItem?.address}”进行估值失败，失败原因：无法获得收费标准"
        }
    }

    // 完成计费
    def finish(ExecStatusDict status, String reason)
    {
        this.status = status
        if (status.code == "-1")
        {
            this.description = "${this.description}，失败原因：${reason}"
        }
        if (status.code == "1")
        {
            def f = Fees.findByCode('EstateEvaluate')
            def fd = FeesDetail.findByFeesAndBuildingType(f, this.buildingType)
            this.amount = fd.unitPrice
        }

        this.modifiedDate = new Date()
        this.finishAt = new Date()
        this.save()
    }
}
